
package com.base.myproject.client.busi.storeslist;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseStoreEvent;
import com.base.myproject.client.busi.BaseStoreEvent.EventType;
import com.base.myproject.client.busi.BaseStoreListener;
import com.base.myproject.client.busi.BO.BaseObject;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.business.BusinessRatify;
import com.base.myproject.client.busi.ccode.CcodeSelect;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.system.SystemMessage;
import com.base.myproject.client.tools.CommandPrepareCall;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncMakeCode;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.base.myproject.client.tools.SendServlet;
import com.base.myproject.client.ui.BaseBusiLeftPanel;
import com.base.myproject.client.ui.BaseChileGridPanel;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseSimpleCombox;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * 入库通知
 * 有两种方式 mode 0是默认有左边窗口方式，1是对话框方式
 * @author zgw
 *2009-9-3 上午11:12:30
 * javazgw@gmail.com
 */
public class StoreinPre extends BaseJDBCPanel  implements BusinessRatify,AsyncCallback<CommandSyncReturnObject>{
	 BaseStore<BusiObject> store = null;
	 BaseGrid grid = null;
	 Dialog dialog;
	 int mode = 0; //1 是对话框方式，默认是有左边窗口方式
	//业务代码
	//final static String
	 
	 BaseChileGridPanel bcgp;
	public StoreinPre() {
		super();
		init();
		//this.setScrollMode(Scroll.AUTO);
	}
/**
 * 为了插入记录建立的构造函数，参数暂时没用
 * 直接调用 insertItem（）生成新纪录
 * @param newitem
 * @wbp.parser.constructor
 */
	public StoreinPre(String newitem)
	{
		mode= 1;
		
	}
	public void init() {
		 busi =SystemMessage.busi[3];
	
//		this.setAutoHeight(true);
		 this.setAutoHeight(true);
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
	    centerData.setMargins(new Margins(5, 5, 0, 0));  
	    this.add(initWeigetx(),centerData);
		
		
		registerMainStore(store);
		registerStore(store);
		
		this.setBusino(busi);
		setdefault();
		
		cgcodetext.addListener(Events.Changed, getListener());
		
	}
	
	public void StoreoutPreDialog()
	{
		 busi =SystemMessage.busi[3];
		store =  new BaseStore<BusiObject>();
		store.addBaseStoreListener( EventType.ROW_ADDED, getBasestoreListener());
		
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.addField("busicode"); 
		type.addField("bcode");
		type.addField("ccode");
		type.addField("busistatus");
		type.addField("vprepare");
		type.addField("predate");
		type.addField("modifydate");
		type.addField("codename");
		type.addField("odate");
		type.addField("mark");
		type.addField("scode");
		 type.addField("deeldate");
		 type.getField("deeldate").setType(Date.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("storeslist");
		info.setKeyname(new String[]{"icode"});
		store.setType(type);
		store.setStoreinfo(info);
		setdefault();
//		store.setKeyname(new String[]{"icode"});
//		store.setTablename("storeslist");
		
		 dialog = new Dialog();
		 dialog.setHeading("商品入库");
		 dialog.setIconStyle("images//16//feasibility_study.png");
	//	dialog.setLayout(new CenterLayout());
//		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 250);
//		westData.setSplit(true);
//		westData.setCollapsible(true);
//		westData.setMargins(new Margins(0));
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 0, 5, 0));
//		panel.add(CreateCcodeLeftPanel(), westData);

		TabPanel tabpanel = new TabPanel();
		//tabpanel.setWidth(450);
	//	tabpanel.setHeight(450); 
		tabpanel.setAutoHeight(true);
		TabItem normal = new TabItem("基本信息");
		
		VerticalPanel verticalpanel = new VerticalPanel();
		
		verticalpanel.add(creatContract());
		verticalpanel.add(CreateGCodePanel());

	
		this.add(verticalpanel, centerData);
		this.setSize(640, 630);
		dialog.add(this);
//		BaseMainPanel.getInstance().setHeight("760");
		
		dialog.setSize(680, 680);
		dialog.setTitle("商品入库");
		dialog.getButtonById(Dialog.OK).setText("提交确认");
		dialog.getButtonById(Dialog.OK).addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
			
				ratify();
			}
			
		});
		dialog.show();
		store.addX();
		
	}
	
	private BaseStoreListener getBasestoreListener()
	{
		BaseStoreListener listener = new BaseStoreListener()
		{

			@Override
			public void BaseStoreEventHandle(final BaseStoreEvent evt) {
				
				if(evt.getEventType().equals(BaseStoreEvent.EventType.ROW_ADDED))
				{
					
					System.out.println("rowadd....");
					
					final int rowindex = evt.getBasestore().getCurcowindex();
					
					 CommandSyncContainer list  = new CommandSyncContainer();
					 CommandSyncMakeCode csmc = new CommandSyncMakeCode();
					 
					 csmc.setBeginstr("入库");
					 csmc.setColumnname("codename");
					 csmc.setTablename("storeslist");
					 list.add(csmc);
					 
					 
					
					 final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
						.create(com.base.myproject.client.tools.GreetingService.class);
				greetingService.SendCommandSync(list, new AsyncCallback<CommandSyncReturnObject>() {
					public void onFailure(Throwable caught) {
						Window.alert("网络连接不稳定，请稍后重试！");
					}

					public void onSuccess(CommandSyncReturnObject cyro ) {
						
						System.out.println("成功："+cyro.getMessage()+cyro.isB()+cyro.getRetrunstr());
						if(cyro.isB())
						evt.getBasestore().setValue(rowindex, "codename", cyro.getRetrunstr());
						else
						{
							Window.alert("分配内码时出错。");
						}
					}
				});
				}
			}

			@Override
			public void onBrowserEvent(Event event) {
				
				
			}
			
		};
		
		return listener;
	}
	protected void save()
	{
		List<BaseObject> l =getMainstore().getModels();
		Date daynow = new Date();
		DateTimeFormat fmt = DateTimeFormat.getFormat("MM/dd/yyyy");
		String date = fmt.format(daynow);
		
		for(BaseObject bo :l)
		{
			
			if((bo.getBusistatus() & BaseObject.INSERT) ==BaseObject.INSERT || (bo.getBusistatus() & BaseObject.UPDATE) ==BaseObject.UPDATE)
			{
				bo.set("vprepare", Env.getEnvbcode());
//				bo.set("predate", Env.getEnvbcode());
				bo.set("modifydate", date);
				
			}
		}
		
		super.save();
		
	}
	private void setdefault()
	{
		store.addDefault("busicode", busi);
		store.addDefault("bcode", Env.getEnvbcode());
		store.addDefault("inoutflag", "1");
		store.addDefault("busistatus","1");
		
	}
	public void complete()
	{
		super.complete();
		ttiratify.setText("提交确认");
		getToolbarll().add(ttiratify);
	}
	
	private Widget initWeigetx()
	{
	VerticalPanel v = new VerticalPanel();
	HorizontalPanel h= new HorizontalPanel();
	v.setHorizontalAlign(HorizontalAlignment.LEFT);
	h.setVerticalAlign(VerticalAlignment.TOP);
	h.add(CreateCcodeLeftPanel());
	v.add(creatContract());
	v.add(CreateGCodePanel());
	h.add(v);
	h.setSpacing(5);
	return h;
	
}
//	public ContentPanel initWeiget() {
//		ContentPanel panel = new ContentPanel();
//		panel.setHeaderVisible(false);
//		panel.setLayout(new BorderLayout());
//		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 300);
//		westData.setSplit(true);
//		westData.setCollapsible(true);
//		westData.setMargins(new Margins(10));
//		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
//		centerData.setMargins(new Margins(5, 0, 5, 0));
//		panel.add(CreateCcodeLeftPanel(), westData);
//
//		TabPanel tabpanel = new TabPanel();
//		//tabpanel.setWidth(450);
//	//	tabpanel.setHeight(450); 
//		//tabpanel.setAutoHeight(true);
//		TabItem normal = new TabItem("基本信息");
//		
//		VerticalPanel verticalpanel = new VerticalPanel();
//		
//		verticalpanel.add(creatContract());
//		verticalpanel.add(CreateGCodePanel());
//		//Scroll scroll = new Scroll("20");
//		
//		//verticalpanel.setScrollMode(Scroll.ALWAYS);
//		//verticalpanel.setHeight("10000px");
//		normal.add(verticalpanel);
//	/*	TabItem gcode = new TabItem("商品信息");
//		gcode.add(CreateGCodePanel());*/
//		tabpanel.add(normal);
//		
//		tabpanel.setAnimScroll(true);
////		tabpanel.add(gcode);
//		panel.add(tabpanel, centerData);
//		panel.setHeaderVisible(false);
//		verticalpanel.setHeight("430");
//		panel.setHeight("480");
//		normal.setHeight("430");
//		tabpanel.setHeight("460");
//		//BaseMainPanel.getInstance().setHeight("760");
//		return panel;
//	}
/**
 * 入库管理左边查询
 * @return
 */
	public ContentPanel CreateCcodeLeftPanel() {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		ColumnConfig cname = new ColumnConfig("codename", "入库单号", 200);
		TextField<String> gnametext = new TextField<String>();

		columns.add(cname);
		
//		ColumnConfig icode	 = new ColumnConfig("icode", "icode", 50);
//
//		columns.add(icode);
//		columns.add(new ColumnConfig("busistatus", "busistatus", 50));
//		DateField dateField = new DateField();

		ColumnModel cm = new ColumnModel(columns);

		// --set modetype
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.addField("busicode"); 
		type.addField("bcode");
		type.addField("ccode");
		type.addField("busistatus");
		type.addField("vprepare");
		type.addField("predate");
		type.addField("modifydate");
		type.addField("codename");
		type.addField("odate");
		type.addField("mark");
		type.addField("scode");
		 type.addField("deeldate");
		 type.getField("deeldate").setType(Date.class);
		// type.addField("imagepath");
		StoreInfo info = new StoreInfo();
		info.setTablename("storeslist");
		info.setKeyname(new String[]{"icode"});
		info.setBusicode(busi);
		info.setSql("select * from storeslist where busicode ='"+busi+"' and inoutflag=1 and {PERMISSION}");
//		info.setKeyname(new String[] { "codename" });
		
		BaseBusiLeftPanel<BusiObject> basbusileftpanel = new BaseBusiLeftPanel<BusiObject>(columns,
				type, info);
		store = basbusileftpanel.getStore();
		store.addBaseStoreListener( EventType.ROW_ADDED, getBasestoreListener());
		basbusileftpanel.getGrid().setWidth("260");
		basbusileftpanel.getGrid().setAutoExpandColumn("codename");
		setMaincomponent(basbusileftpanel.getGrid());
		return basbusileftpanel.getPanel();
	}
	private ContentPanel creatContract()
	{
		ContentPanel panel = new ContentPanel();
		panel.setHeaderVisible(false);
//		panel.setAutoHeight(true);
//		panel.setAutoWidth(true);
		BaseFormPanel leftpanel = new BaseFormPanel(store);
		TableLayout tl = new TableLayout(4);
		TableData td = new TableData();
		td.setColspan(1);
		td.setRowspan(1);
		td.setPadding(2);
		TableData td2 = new TableData();
		td2.setColspan(3);
		td2.setRowspan(2);
		leftpanel.setLayout(tl);
		
//		leftpanel.setLayout(new FormLayout());
//		FormData fd = new FormData(200, 30);
		leftpanel.setHeaderVisible(false);
		leftpanel.setBodyBorder(false);
		
		BaseFormPanel rightpanel = new BaseFormPanel(store);
		rightpanel.setBodyBorder(false);
		rightpanel.setHeaderVisible(false);
		
		
		TextField<String> codename = new TextField<String>();
		codename.setName("codename");
		codename.setAllowBlank(false);
		codename.setFieldLabel("出库单编号");
		codename.setEnabled(false);

		BaseComboBoxForm<BaseModelData> busistatus = new BaseComboBoxForm("code",
				"name", SystemMessage.getInstance().getStatusBaseStore());

		busistatus.setName("busistatus");
		busistatus.setFieldLabel("单据状态");
		
		
		ModelType bcodetype = new ModelType();
		bcodetype.setRoot("records");
		bcodetype.setRecordName("record");

		bcodetype.addField("code");

		bcodetype.addField("name");

		StoreInfo bcodeinfo = new StoreInfo();

		bcodeinfo.setTablename("bcode");
		bcodeinfo.setSql("select bcode as code,bname as name from bcode");
		BaseStore<BaseModelData> bcodestore = new BaseStore<BaseModelData>(
				bcodetype, bcodeinfo);
		BaseComboBoxForm<ModelData> bcode = new BaseComboBoxForm("code",
				"name", bcodestore);
		
		
		bcode.setName("bcode");

		bcode.setFieldLabel("部门人员");

		
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		type.addField("code");

		type.addField("name");

//		StoreInfo info = new StoreInfo();
//		info.setForcenolimit("true");
//		info.setTablename("ccode");
//		info.setSql("select ccode as code,cname as name from ccode where ccodetype=2");
//		BaseStore<BaseModelData> ccodestore = new BaseStore<BaseModelData>(
//				type, info);
	//	BaseComboBoxForm<BaseModelData> ccode = new BaseComboBoxForm<BaseModelData>("ccode", "cname","ccode","where ccodetype =2",true);
		
		CcodeSelect ccodeselect = new CcodeSelect(2);
		BaseComboBoxForm<BaseModelData> ccode = ccodeselect.getCcodeSelect();
		ccode.setFieldLabel("供应商");
		ccode.setName("ccode");
		 
//		
//		TextField<String> scode = new TextField<String>();
//		scode.setName("scode");
//
//		scode.setFieldLabel("仓库");
		
		ModelType scodetype = new ModelType();
		scodetype.setRoot("records");
		scodetype.setRecordName("record");

		scodetype.addField("code");

		scodetype.addField("name");

		StoreInfo scodeinfo = new StoreInfo();
		scodeinfo.setForcenolimit("true");
		 scodeinfo.setTablename("ccode");
		 scodeinfo.setSql("select code,name from dictionary where type ='2'");
		BaseStore<BaseModelData> scodestore = new BaseStore<BaseModelData>(
				scodetype, scodeinfo);
		BaseComboBoxForm<ModelData> scode = new BaseComboBoxForm("code",
				"name", scodestore);
		scode.setFieldLabel("仓库");
		scode.setName("scode");
		
		DateField  deeldate = new DateField();
		deeldate.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-M-d"));
	
//		deeldate.setValue(null);
		//----系统信息
		
		
		TextArea mark = new TextArea();
		mark.setName("mark");
		mark.setMaxLength(250);
		mark.setFieldLabel("备注");
		mark.setWidth(375);
		leftpanel.add(new Label("入库单编号:"),td);
		leftpanel.add(codename,td);
		leftpanel.add(new Label("部门人员:"),td);
		leftpanel.add(bcode,td);
		leftpanel.add(new Label("单据状态:"),td);
		leftpanel.add(busistatus,td);
		leftpanel.add(new Label("供应商:"),td);
		leftpanel.add(ccode,td);
		leftpanel.add(new Label("仓库:"),td);
		leftpanel.add(scode,td);
		leftpanel.add(new Label("采购日期:"),td);
		leftpanel.add(deeldate,td);
		
//		leftpanel.add(new Label(""),td);//占位
//		leftpanel.add(new Label(""),td);//占位
		leftpanel.add(new Label("备注:"),td);
		leftpanel.add(mark,td2);

		leftpanel.inited();

		
		final BorderLayout layout = new BorderLayout();  
		 panel.setLayout(layout);  
		 BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
		    centerData.setMargins(new Margins(5, 0, 5, 0));  
		 BorderLayoutData southData = new BorderLayoutData(LayoutRegion.SOUTH, 130);  
		    southData.setSplit(true);  
		    southData.setCollapsible(true);  
		    
		    southData.setFloatable(true);  
		    southData.setMargins(new Margins(0, 5, 5, 5));  
			panel.add(leftpanel,centerData);
			//系统信息被屏蔽，采用rpc的方式
//			panel.add(SystemInfo(),southData);
			panel.setWidth(640);
			panel.setHeight("200px");
			panel.setHeaderVisible(false);

			
		return panel;
	}
	
	TextField<String> cgcodetext = new TextField<String>();
	/**
	 * 商品
	 * @return
	 */
	private BaseChileGridPanel CreateGCodePanel() {

		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig cgcode = new ColumnConfig("cgicode", "barcode", 100);
		
		cgcode.setEditor(new CellEditor(cgcodetext));
		columns.add(cgcode);
		
		ColumnConfig barcode = new ColumnConfig("cgcode", "名称", 100);
		TextField<String> barcodetext = new TextField<String>();
		barcode.setEditor(new CellEditor(barcodetext));
		columns.add(barcode);
		
		ColumnConfig modeltype = new ColumnConfig("modeltype", "规格型号", 100);
		TextField<String> modeltypetext = new TextField<String>();
		modeltype.setEditor(new CellEditor(modeltypetext));
		columns.add(modeltype);
		
		
		
		ColumnConfig gcode = new ColumnConfig("gcode", "类型", 100);
		BaseSimpleCombox bsc =new BaseSimpleCombox("gname","gcode","");
		//TextField<String> gcodetext = new TextField<String>();
		
		//gcode.setEditor(new CellEditor(gcodetext));
		gcode.setEditor(bsc.getCellEditor());
		columns.add(gcode);
		
		
		ColumnConfig qty = new ColumnConfig("qty", "数量", 100);
		NumberField qtytext = new NumberField();

		qtytext.setPropertyEditorType(Double.class);
		qty.setEditor(new CellEditor(qtytext));
		columns.add(qty);
		
		ColumnConfig buyprice = new ColumnConfig("buyprice", "购买单价", 70);
		NumberField buypricetext = new NumberField();

		buypricetext.setPropertyEditorType(Double.class);
		buyprice.setEditor(new CellEditor(buypricetext));
		columns.add(buyprice);
		
		ColumnConfig saleprice = new ColumnConfig("saleprice", "销售指导单价", 120);
		NumberField salepricetext = new NumberField();

		salepricetext.setPropertyEditorType(Double.class);
		saleprice.setEditor(new CellEditor(salepricetext));
		columns.add(saleprice);
		
		ColumnConfig saleprice2 = new ColumnConfig("saleprice2", "销售指导单价2", 120);
		NumberField salepricetext2 = new NumberField();

		salepricetext2.setPropertyEditorType(Double.class);
		saleprice2.setEditor(new CellEditor(salepricetext2));
		columns.add(saleprice2);
		
		
		ColumnConfig saleprice3 = new ColumnConfig("saleprice3", "销售指导单价3", 120);
		NumberField salepricetext3 = new NumberField();

		salepricetext3.setPropertyEditorType(Double.class);
		saleprice3.setEditor(new CellEditor(salepricetext3));
		columns.add(saleprice3);
		
		ColumnConfig qtyunit = new ColumnConfig("qtyunit", "单位", 100);
		TextField<String> qtyunittext = new TextField<String>();
		qtyunit.setEditor(new CellEditor(qtyunittext));
		columns.add(qtyunit);
//		ColumnConfig upric = new ColumnConfig("upric", "单价", 100);
//		TextField<String> uprictext = new TextField<String>();
//		upric.setEditor(new CellEditor(uprictext));
//		columns.add(upric);


		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		type.addField("idx");
		type.addField("upicode");
		type.addField("codename");
		type.addField("cgicode");
		type.addField("cgcode");
		type.addField("gcode");
		type.addField("gcode");
		type.addField("qty");
		type.addField("modeltype");
		type.addField("qtyunit");
		type.addField("upric");
		type.addField("scode");
		type.addField("buyprice");
		type.addField("saleprice");
		type.addField("saleprice2");
		type.addField("saleprice3");
		type.getField("qty").setType(Double.class);
		type.getField("buyprice").setType(Double.class);
		type.getField("saleprice").setType(Double.class);
		type.getField("saleprice2").setType(Double.class);
		type.getField("saleprice3").setType(Double.class);
		 type.addField("deeldate");
		 type.getField("deeldate").setType(Date.class);
		StoreInfo info = new StoreInfo();

		info.setTablename("storeslistg");
		info.setSql("select * from storeslistg where upicode='{icode}'");
		info.setParent(store);// 关联主表
//		info.setAutoinccolumn("idx"); // 自增字段，如果子表一定需要
	//	info.setLinkparentcolumns(new String[] { "codename" }); // 关联主表字段
		
		info.setLinkparentcolumns(new String[] { "icode","scode","deeldate" }); // 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "upicode","scode","deeldate" });// 关联主表字段
		info.setForcenolimit("true");
		info.setKeyname(new String[] { "icode" }); // 主键
		
/*		BaseStore contractgcode = new BaseStore<BusiObject>(type, info);
		contractgcode.setName("storeslistg");
		ColumnModel cm = new ColumnModel(columns);
		
		BaseGrid contractgcodegrid = new BaseGrid(contractgcode, cm);
//		  GroupSummaryView summary = new GroupSummaryView();  
		cbs.setSelectionMode(SelectionMode.SINGLE);

		contractgcodegrid.setSelectionModel(cbs);
		contractgcodegrid.setAutoHeight(true);
		contractgcodegrid.setAutoWidth(true);
*/
		bcgp = new BaseChileGridPanel(type,info,columns,BaseChileGridPanel.QUERY_MODE);
		bcgp.grid.setAutoHeight(false);
		bcgp.grid.setAutoWidth(false);
		bcgp.grid.setHeight(230);
		bcgp.grid.setWidth(640);
		bcgp.setHeaderVisible(false);
		bcgp.grid.getView().setForceFit(true);
		bcgp.setHeaderVisible(false);
		bcgp.grid.getStore().addDefault("inoutflag", "1");
		
		bcgp.grid.addListener(Events.AfterEdit,getListener() );
		/*		bcgp.setType(type);
		bcgp.setStoreinfo(info);
		bcgp.setColumns(columns);*/
		
		
		registerStore(bcgp.getStore());
		
		
		return bcgp;
	}
	
	/**
	 * 系统信息
	 * @return
	 */
	private ContentPanel SystemInfo()
	{
		 ContentPanel noth = new ContentPanel();
		 BaseFormPanel leftpanel = new BaseFormPanel(getStore());
			leftpanel.setBodyBorder(false);
			leftpanel.setHeaderVisible(false);
			
			BaseFormPanel rightpanel = new BaseFormPanel(getStore());
			rightpanel.setBodyBorder(false);
			rightpanel.setHeaderVisible(false);
			
			
			//-----------------
			TextField<String> vprepare = new TextField<String>();
			vprepare.setName("vprepare");
			vprepare.setEnabled(false);
			vprepare.setFieldLabel("制单人");
			TextField<String> predate = new TextField<String>();
			predate.setName("predate");
			predate.setEnabled(false);
			predate.setFieldLabel("制单日期");
			TextField<String> modifydate = new TextField<String>();
			modifydate.setName("modifydate");
			modifydate.setEnabled(false);
			modifydate.setFieldLabel("最近修改日期");
			
			TextField<String> odate = new TextField<String>();
			odate.setName("odate");
			odate.setEnabled(false);
			odate.setFieldLabel("提交审批日期");

			
			//-----------------
			leftpanel.add(vprepare);
			rightpanel.add(predate);
				leftpanel.add(modifydate);
				rightpanel.add(odate);
			
			TableData td = new TableData();
			td.setColspan(1);
			td.setRowspan(1);
			// 对齐方式
			td.setHorizontalAlign(HorizontalAlignment.LEFT);
			td.setVerticalAlign(VerticalAlignment.TOP);
			leftpanel.inited();
			rightpanel.inited();
			FieldSet fieldSet = new FieldSet();
		//fieldSet.setHeading("单据信息");
			fieldSet.setLayout(new TableLayout(2));
			fieldSet.add(leftpanel,td);
			fieldSet.add(rightpanel,td);
			noth.setHeight("400px");
			noth.add(fieldSet);
			noth.setHeading("单据信息");
		    return noth;
	}
	
	/**
	 * @return the store
	 */
	public BaseStore<BusiObject> getStore() {
		return store;
	}
	/**
	 * @param store the store to set
	 */
	public void setStore(BaseStore<BusiObject> store) {
		this.store = store;
	}
	/**
	 * 给单据插入记录，通常用于数据拷贝，由上游数据生成
	 * @param mainModelData
	 * @param ChileModelData
	 */
	public  void insertItem(List<BusiObject> mainModelData,List<BusiObject> ChileModelData)
	{
		SendServlet ss = new SendServlet();
		String codename = ss.getStringx("/"+GWT.getModuleName()+"/BaseServlet", "Action=makecodename&table=storeslist&column=codename&beginstr=test");
		java.util.Vector<String > v = new java.util.Vector<String>();
		for(BusiObject md:mainModelData)
		{
			//String codename ="12323";
			String busicode = busi;
			int inoutflag = 1;
			String bcode =Env.getEnvbcode();
			String ccode =md.get("ccode");
			String vprepare=md.get("vprepare");
			String busistatus =md.get("busistatus");
			String modifydate = md.get("modifydate");
			String sql = "insert storeslist (codename,busicode,inoutflag,bcode,ccode,vprepare,busistatus,modifydate) values('"+codename+"','"+busicode+"','"+inoutflag+"','"+bcode+"','"+ccode+"','"+vprepare+"','"+busistatus+"','"+modifydate+"')";
			v.add(sql);
		}
		
		for(BusiObject cd:ChileModelData)
		{
			//String codename ="12323";
			String busicode = busi;
			int inoutflag = 1;
			String bcode =Env.getEnvbcode();
			String ccode = cd.get("ccode");
			String cgicode=cd.get("cgicode");;
			String gcode=cd.get("gcode");;
			String scode=cd.get("scode");;
			String qty=cd.get("qty");;
			String qtyunit=cd.get("qtyunit");;
			String upicode=cd.get("upicode");;
		
			String sql = "insert storeslistg (codename,busicode,inoutflag,bcode,ccode,cgicode,gcode,scode,qty,qtyunit,upicode) values('"+codename+"','"+busicode+"','"+inoutflag+"','"+bcode+"','"+ccode+"','"+cgicode+"','"+gcode+"','"+scode+"','"+qty+"','"+qtyunit+"','"+upicode+"')";
			v.add(sql);
		}
		
		
		ss.ExcJDBCSQL(v);
	}
	protected void ratify()
	{
	
		
		if(BeforeRatify())
		{
			if(OnRatify())
			{
				AfterRatify();
			}
		}
	}
	@Override
	public boolean AfterRatify() {
		
		
		
		return true;
	}
	@Override
	public boolean BeforeRatify() {
		
		return true;
	}
	@Override
	public boolean OnRatify() {
		this.mask("提交....");
		int index = store.getCurcowindex();
		String icode = store.getValue(index, "icode").toString();
		//store._trySave(index);
		String sql = "update storeslist set busistatus='131072' where icode ='"
				+ icode + "' and  inoutflag=1";
		CommandSyncContainer list = new CommandSyncContainer();
		CommandSyncsql commandsql = new CommandSyncsql();
		CommandPrepareCall commandpc = new CommandPrepareCall();
		
		commandsql.getV().addAll(store._trySave(index));
		commandsql.getV().add(sql);
		

		list.add(commandsql);
		
//		CommandSyncCopyUP cu = new CommandSyncCopyUP();
//		cu.setIcode(icode);
//		
//		list.add(cu);

		//入库通知提交后调用存储过程
		commandpc.setCallname("storeinratify (?)");
		commandpc.addItem(icode, CommandPrepareCall.STRING);
		commandpc.setContinueorrollback(false);
		list.add(commandpc);
		
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.SendCommandSync(list,
				this);
		
	
		
		//store.trySave(index);
		return false;
	}
	@Override
	public void onFailure(Throwable caught) {
		MessageBox.alert("错误", "网络出现连接失败！请重新提交。", null);
		this.unmask();// 提交的时候mask，现在取消mask
		
	}
	@Override
	public void onSuccess(CommandSyncReturnObject cyro) {
		if(cyro.isB())
		{
			store.setValue("busistatus", "131072");
			store.commitChanges();
			Info.display("", "提交成功");
			if(mode==1)
			{
			this.dialog.setVisible(false);
			store=null;
			this.grid = null;
			}
		}
		else
		{
			MessageBox.alert("错误", cyro.getMessage(), null);
		}
		this.unmask();// 提交的时候mask，现在取消mask
		
	}
	
	void _setValue(int index,DataSet ds)
	{

		if (ds.getRowCount()>0) {
			
			bcgp.getStore().setValue(index,"cgcode", ds.getValue(0, "cgcode"));
			bcgp.getStore().setValue(index,"modeltype", ds.getValue(0, "modeltype"));
			bcgp.getStore().setValue(index,"gcode", ds.getValue(0, "gcode"));
			bcgp.getStore().setValue(index,"qtyunit", ds.getValue(0, "qtyunit"));
			bcgp.getStore().setValue(index,"buyprice", ds.getValue(0, "buyprice"));
			bcgp.getStore().setValue(index,"saleprice", ds.getValue(0, "saleprice"));
			bcgp.getStore().setValue(index,"saleprice2", ds.getValue(0, "saleprice2"));
			bcgp.getStore().setValue(index,"saleprice3", ds.getValue(0, "saleprice3"));
		}
		else
		{
			bcgp.getStore().setValue(index,"cgcode", null);
			bcgp.getStore().setValue(index,"modeltype", null);
			bcgp.getStore().setValue(index,"gcode", null);
			bcgp.getStore().setValue(index,"qtyunit", null);
			bcgp.getStore().setValue(index,"buyprice", null);
			bcgp.getStore().setValue(index,"saleprice",null);
			bcgp.getStore().setValue(index,"saleprice2", null);
			bcgp.getStore().setValue(index,"saleprice3",null);
		}
		
	}
	/**
	 * 判断是否选择库里已经有的商品。
	 */
	public void selectCGcode(final int index)
	{
		Object cgicode = bcgp.getStore().getValue(index,"cgicode");
		if(cgicode!=null)
		{
			String sql = "select * from cgcode where cgicode='"+cgicode+"'";
			final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
			.create(com.base.myproject.client.tools.GreetingService.class);
			greetingService.getDataSet(sql,
			new AsyncCallback<DataSet>() {

				@Override
				public void onFailure(Throwable caught) {

				}

				@Override
				public void onSuccess(DataSet result) {

					_setValue(index,result);
			
					
					
				}

			});
			
		}
	}
	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;                               
                                                                     
	Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null)                                        
		{                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                      
				try {                                                        
					if (sync == 1) {                                           
						if (be.getType() == Events.Select) {                     
							if (be.getSource().equals("")) {                            
                                                                     
							}                                                      
						} else if (be.getType() == Events.Changed) {              
//							if (be.getSource().equals(cgcodetext)) {         
//								selectCGcode(bcgp.getStore().getCurcowindex());                       
//							}                                                      
						} else if (be.getType() == Events.KeyUp) {               
							if (be instanceof ComponentEvent) {                    
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{                                                    
								}                                                    
							}                                                      
						} 
						else if(be.getType() ==Events.AfterEdit)
						{
							GridEvent<BusiObject> be2 = (GridEvent<BusiObject>)be;
							if (be2.getProperty().equals("cgicode")) {
								selectCGcode(be2.getRowIndex());  
							}
						}
					}                                                          
				} finally {                                                  
					sync--;                                                    
				}                                                            
			}                                                              
                                                                     
		};                                                               
		return defaultlistener;                                          
	}                                                                  

	
}